package com.wwcom.darteagle.model;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.util.Log;

public class MatchInfo {
	protected long id;
	protected long date;
	private int numLegs;
	private int numSets;
	private int legTarget; 
	private boolean doubleIn;
	private boolean doubleOut;
	private boolean trebleIn;
	private boolean trebleOut;
	private boolean loserStarts;	
	private List<MatchPlayer> matchPlayers;
	
	private static final Checkout checkout= Checkout.getSingletonInstance();
	
	public MatchInfo() {
		initialiseSelf();
	}	
	
	public MatchInfo(int numLegs, int numSets, int legTarget, 
			boolean doubleIn, boolean doubleOut, boolean trebleIn, boolean trebleOut) {
		initialiseSelf();
		this.numLegs= numLegs;
		this.numSets= numSets;
		this.legTarget= legTarget;
		this.doubleIn= doubleIn;
		this.doubleOut= doubleOut;
		this.trebleIn= trebleIn;
		this.trebleOut= trebleOut;
		this.loserStarts= false;		
	}

	private void initialiseSelf() {
		numLegs= 3;
		numSets= 0;
		legTarget= 501;
		doubleIn= false;
		doubleOut= true;
		trebleIn= false;
		trebleOut= false;
		loserStarts= false; 
		matchPlayers= new ArrayList<MatchPlayer>();
		id= -1;
		date= Calendar.getInstance().getTimeInMillis();
		Log.d("MatchInfo","Default date: "+date);		
	}
	
	//accessors
	public void setId(long id) {
		this.id= id;
	}
	
	public long getId() {
		return id;
	}
	
	public void setDate(long date) {
		this.date= date;
	}
	
	public long getDate() {
		return date;
	}
	
	public int getNumLegs() {
		return numLegs;
	}

	public void setNumLegs(int numLegs) {
		this.numLegs = numLegs;
	}

	public int getNumSets() {
		return numSets;
	}

	public void setNumSets(int numSets) {
		this.numSets = numSets;
	}

	public int getLegTarget() {
		return legTarget;
	}

	public void setLegTarget(int legTarget) {
		this.legTarget = legTarget;
	}

	public boolean isDoubleIn() {
		return doubleIn;
	}

	public void setDoubleIn(boolean doubleIn) {
		this.doubleIn = doubleIn;
	}

	public boolean isDoubleOut() {
		return doubleOut;
	}

	public void setDoubleOut(boolean doubleOut) {
		this.doubleOut = doubleOut;
	}

	public boolean isTrebleIn() {
		return trebleIn;
	}

	public void setTrebleIn(boolean trebleIn) {
		this.trebleIn = trebleIn;
	}

	public boolean isTrebleOut() {
		return trebleOut;
	}

	public void setTrebleOut(boolean trebleOut) {
		this.trebleOut = trebleOut;
	}
	
	public boolean isLoserStarts() {
		return loserStarts;
	}
	
	public void setLoserStarts( boolean loserStarts ) {
		this.loserStarts= loserStarts;
	}

	public String[] getCheckout(int score, int nDarts) {
		return checkout.getCheckout(score,nDarts,doubleOut,trebleOut);
	}
	
	//players
	public List<MatchPlayer> getMatchPlayers() {
		return matchPlayers;		
	}
	
	public void addMatchPlayer(Player player) {
		MatchPlayer matchPlayer= new MatchPlayer(player);
		matchPlayers.add(matchPlayer);
	}
	
	public void removeMatchPlayer(Player player) {
		for (int i= 0; i<matchPlayers.size(); i++) {
			if (matchPlayers.get(i).getPlayer().getName() == player.getName()) {
				matchPlayers.remove(i);
				break;
			}
		}
	}

	//messages and display
	public String getMatchDescription() {
		String matchDesc= "";
		
		matchDesc+= Integer.toString(matchPlayers.size())+" player";
		if (matchPlayers.size() != 1)
			matchDesc+= "s";
		
		matchDesc+= ".  "+Integer.toString(getLegTarget());
		if (isDoubleIn()) {
			matchDesc+= ", double in";
		} else if (isTrebleIn()){
			matchDesc+= ", treble in";			
		}
		if (isDoubleOut()) {
			matchDesc+= ", double out";			
		} else if (isTrebleOut()){
			matchDesc+= ", treble out";			
		}
		
		if (loserStarts) {
			matchDesc+= ", loser throws first";
		}
		matchDesc+= ". ";			
		
		int numSets= getNumSets();
		int numLegs= getNumLegs();
		if (numSets>0) {
			matchDesc+= "First to "+Integer.toString(numSets)+" sets, "+Integer.toString(numLegs)+
					" legs per set.";
		} else {
			matchDesc+= "First to "+Integer.toString(numLegs)+" legs.";
		}
		return matchDesc;
	}


}
